
(function($) {
	$.fn.page = function(options) {
		var details = {
			"totalPage": 0,
			"pageNo": 1,
			"callBack": function(index) { }
		};
		var settings = $.extend({}, details, options);
		var parentDom = $(this),
			pageHtml = '';

		function init() {
			parentDom.html(pageHtml);
			
			if(settings.totalPage < 1){
				return;
			}
			settings.pageNo = parseInt(settings.pageNo);
			var html = '共 ' + settings.totalPage + ' 条&nbsp;';
			
			if(settings.pageNo != 1){
				html += '<span class="btn btn-primary btn-sm pages btn-pg" data-lang="1">首 页</span>';
				html += '<span class="btn btn-primary btn-sm pages btn-pg" data-lang="'+(settings.pageNo-1)+'>上一页</span>';
			} else {
				html += '<span class="btn btn-default btn-sm pages btn-pg">首 页</span>';
				html += '<span class="btn btn-default btn-sm pages btn-pg">上一页</span>';
			}
			
			if(settings.totalPage <= 5) {
				for (var i = 1; i <= settings.totalPage; i++) {
					html += '<span class="btn btn-sm btn-pg pages ';
					if (i == settings.pageNo) {
						html += 'btn-default';
					} else {
						html += 'btn-primary';
					}
					html += '">' + i + '</span>';
				}
			} else {
				if (settings.pageNo > 3) {
					html += '<span class="btn btn-default btn-sm pages btn-pg" >...</span>';
				}
				for (var i = -2; i < 3; i++) {
					var p = settings.pageNo + i;
					if (p > 0 && p <= settings.totalPage) {
						html += '<span class="btn btn-sm pages btn-pg ';
						if (i == 0) {
							html += 'btn-default';
						} else {
							html += 'btn-primary';
						}
						html += '>' + i + '</span>';
					}
				}
				
				if (settings.pageNo + 2 > settings.totalPage) {
					html += '<span class="btn btn-default btn-sm pages btn-pg">...</span>';
				}
			}	
	
			if (settings.totalPage <= 1 || settings.totalPage == settings.pageNo) {
				html += '<span class="btn btn-default btn-sm pages btn-pg" ">下一页</span>';
				html += '<span class="btn btn-default btn-sm pages btn-pg" ">尾 页</span>';
			} else {
				html += '<span class="btn btn-primary btn-sm pages btn-pg" data-lang="'+(settings.pageNo+1)+'">下一页</span>';
				html += '<span class="btn btn-primary btn-sm pages btn-pg" data-lang="'+(settings.totalPage)+'">尾 页</span>';
			}

			html += '&nbsp;当前 ' + settings.pageNo + '/' + settings.totalPage + ' 页 &nbsp;转到第';
			html += '<input type="text" style="width:50px" class="_positive_integer">页';
			if (settings.totalPage > 1) {
				html += '<span id="_goPage" class="btn btn-primary btn-sm pages gopage" >转</span>';
			} else {
				html += '<span id="_goPage" class="btn btn-default btn-sm pages gopage" >转</span>';
			}

			parentDom.append('<tbody><tr><td align="center" class="pn-sp" style="height: 90px;">' + html + '</td></tr></tbody>');
			parentDom.removeClass('hid');
		}
		
		// 预绑定
		parentDom.on('click', 'input.btn-primary', function() {
			settings.callBack($(this).data("lang"));
		});
		parentDom.on('click', 'input.gopage', function() {
			settings.callBack($(this).prev().val());
		});

		init();
		var operate = {
			"fresh" : function(pageNo,totalPage){
				if(0 <= pageNo){
					settings.pageNo = pageNo;
				}
				if(0 <= totalPage){
					settings.totalPage = totalPage;
				} 
				init();
			},
			"destroy" : function(){
				parentDom.find("a.pages").unbind();
			}
		};
		return operate;
	};
})(jQuery);